<?php

class categories extends admin_controller {

    //put your code here
    public function __construct() {
        parent::__construct();
        $this->load->model('admin/categories_model', 'cats');
    }

    public function index() {
        $data['result'] = $this->cats->filter(false, false, false, array('parent' => '0'));
        $data['pagination'] = $this->admin_pagination('index', $this->cats->filter(true, false, false, array('parent' => '0')), 10);
        $this->themes->render('categories/index', $data);
    }

    public function view($id = FALSE) {

        if (is_numeric($id)) {
            $falid_id = $this->cats->is_found(array('id' => $id), array(), 'row', __CLASS__);
            $data['result'] = $falid_id;
            $data['additionals'] = get_cat_additional($id);
            $this->themes->render('categories/view', $data);
        } else {
            show_404();
        }
    }

    public function edit($id = false) {
        if ($id) {
            $pass['language'] = $languages = get_languages();
            $id_id = $this->cats->is_found(array('id' => $id), array(), 'row', __CLASS__);
            $pass['cat'] = $id_id;
            $pass['maincats'] = $this->cats->get(array('parent' => '0'));
            if ($this->input->post()) {
                $this->session->set_userdata('post', $this->input->post());
                $this->session->set_userdata('post', $this->input->post());
                $this->form_validation->set_rules('code', lang('label_name'), 'trim|callback_title_required');
                $this->form_validation->set_rules('maincat', lang('label_main_cat'), 'trim|required|numeric');
                $this->form_validation->set_rules('additional', lang('label_cat_additional'), 'trim|numeric');
                if ($this->form_validation->run()) {
                    $data['parent'] = $this->input->post('maincat');
                    $data['user_update'] = $this->admin_id;
                    $data['category_id'] = $id;
                    $data['title'] = serialize($this->input->post('title'));
                    if ($this->input->post('color')) {
                        $data['need_color'] = 'YES';
                    } else {
                        $data['need_color'] = 'NO';
                    }

                    if ($this->input->post('size')) {
                        $data['need_size'] = 'YES';
                    } else {
                        $data['need_size'] = 'NO';
                    }

                    if ($this->input->post('size')) {
                        $data['need_size'] = 'YES';
                    } else {
                        $data['need_size'] = 'NO';
                    }

                    if ($this->input->post('maincat') && $this->input->post('additional')) {
                        $data['additional'] = $this->input->post('additional');
                    } else {
                        $data['additional'] = '0';
                        delete_additional($id);
                    }

                    if ($this->cats->save($data, array('id' => $id), TRUE)) {
                        $this->session->unset_userdata('post');
                        $this->themes->set_message(array('url' => __CLASS__, 'type' => 'success', 'message' => 'message_edit_success'));
                    } else {
                        get_save_error();
                    }
                } else {
                    get_validation_errors();
                }
            } else {
                $this->themes->render('categories/edit', $pass);
            }
        } else {
            show_404();
        }
    }

    public function title_required() {
        $check = FALSE;
        $lang = get_languages();
        $title = $this->input->post('title');
        foreach ($lang as $l) {
            if (isset($title[$l->code]) && $l->default == 'YES') {
                $check = TRUE;
                break;
            }
        }
        if ($check) {
            return TRUE;
        } else {
            $this->form_validation->set_message('title_required', lang('message_title_required'));
            return FALSE;
        }
    }

    public function add() {
        $data['language'] = $languages = get_languages();
        $data['maincats'] = $this->cats->get(array('parent' => '0'));
        if ($this->input->post()) {
            $this->session->set_userdata('post', $this->input->post());
            $this->form_validation->set_rules('code', lang('label_name'), 'trim|callback_title_required');
            $this->form_validation->set_rules('maincat', lang('label_main_cat'), 'trim|required|numeric');
            $this->form_validation->set_rules('additional', lang('label_cat_additional'), 'trim|numeric');
            if ($this->form_validation->run()) {
                $data['parent'] = $this->input->post('maincat');
                $data['user_id'] = $this->admin_id;
                $data['user_update'] = $this->admin_id;
                $data['title'] = serialize($this->input->post('title'));
                if ($this->input->post('color')) {
                    $data['need_color'] = 'YES';
                } else {
                    $data['need_color'] = 'NO';
                }

                if ($this->input->post('size')) {
                    $data['need_size'] = 'YES';
                } else {
                    $data['need_size'] = 'NO';
                }

                if ($this->input->post('size')) {
                    $data['need_size'] = 'YES';
                } else {
                    $data['need_size'] = 'NO';
                }

                if ($this->input->post('maincat') && $this->input->post('additional')) {
                    $data['additional'] = $this->input->post('additional');
                } else {
                    $data['additional'] = '0';
                }
                if ($this->cats->save($data)) {
                    $this->session->unset_userdata('post');
                    $this->themes->set_message(array('url' => __CLASS__, 'type' => 'success', 'message' => 'message_add_success'));
                } else {
                    get_save_error();
                }
            } else {
                get_validation_errors();
            }
        } else {
            $this->themes->render('categories/add', $data);
        }
    }

    public function all() {
        $id = $this->input->post('id');
        $doaction = $this->input->post('doaction');
        // valid if no item select 
        $this->empty_id_actionall($id);
        $this->empty_id_actionall($doaction, 'message_select_action_from_dropdown');

        switch ($doaction) {
            case 'delete': {
                    $this->cats->delete(array('id' => $id), array('parent' => $id));
                    $this->themes->set_message(array('url' => $this->agent->referrer(), 'type' => 'success', 'message' => 'message_delete_success'));
                }
                break;
            case 'no_trash': {
                    $data['trash'] = 'NO';
                    $this->cats->save($data, array('id' => $id), TRUE);
                    $this->themes->set_message(array('url' => $this->agent->referrer(), 'type' => 'success', 'message' => 'message_edit_success'));
                }
                break;
            case 'delete_final': {
                    $this->cats->delete(array('id' => $id), FALSE);
                    delete_additional($id);
                    $this->themes->set_message(array('url' => $this->agent->referrer(), 'type' => 'success', 'message' => 'message_edit_success'));
                }
                break;

            default:
                show_404();
                break;
        }
    }

    public function check_multititle() {
        $lang = get_languages();
        $title = $this->input->post('title');
        $num = (int) $this->session->userdata('numbers');
        $type = $this->input->post('type');
        $data = $this->input->post('data');
        for ($i = 1; $i <= $num; $i++) {
            $check = FALSE;
            foreach ($lang as $l) {
                if ($l->default == 'YES') {
                    if (isset($title[$i][$l->code]) && $title[$i][$l->code] != '') {
                        $check = TRUE;
                    }
                    if (in_array($type[$i], array('list', 'checkbox'))) {
                        if (isset($data[$i][$l->code]) && $data[$i][$l->code] != '') {
                            $check = TRUE;
                        } else {
                            $check = FALSE;
                        }
                    }
                }
            }

            if (!$check) {
                break;
            }
        }
        if ($check)
            return TRUE;
        $this->form_validation->set_message('check_multititle', lang('message_wrong_data2'));
        return FALSE;
    }

    function additional_data($id = false) {
        if (is_numeric($id)) {
            $pass['language'] = $languages = get_languages();
            $pass['cat'] = $id_id = $this->cats->is_found(array('id' => $id, 'additional != ' => '0'), array(), 'row', __CLASS__);
            $pass['additionals'] = $additional = get_cat_additional($id);
            if ($this->input->post()) {
                $this->session->set_userdata('post', $this->input->post());
                $title = $this->input->post('title');
                $type = $this->input->post('type');
                $da = $this->input->post('data');
                $code = $this->input->post('codez');
                $this->load->library('form_validation');
                $rule = '';
                $this->session->set_userdata('numbers', $id_id->additional);
                $this->form_validation->set_rules('code', lang('sa'), 'trim|required|callback_check_multititle');
                if ($this->form_validation->run()) {
                    delete_additional($id);
                    for ($i = 1; $i <= (int) $id_id->additional; $i++) {
                        if (isset($title[$i]) && isset($type[$i])) {
                            $data = NULL;
                            $data['category_id'] = $id;
                            $data['title'] = serialize($title[$i]);
                            $data['type'] = $type[$i];
                            if (isset($code[$i]) && !empty($code[$i])) {
                                $data['code'] = $code[$i];
                            } else {
                                $data['code'] = md5($data['title'] . $data['type'] . time() . $id);
                            }

                            if (in_array($type[$i], array('list', 'checkbox'))) {
                                if (isset($da[$i])) {
                                    $data['data'] = serialize($da[$i]);
                                } else {
                                    $this->themes->set_message(array('url' => current_url(), 'type' => 'error', 'message' => 'message_data_not_comp'));
                                }
                            } else {
                                $data['data'] = '';
                            }
                        }
                        save_cat_additional($data);
                    }
                    $this->session->unset_userdata('post');
                    $this->themes->set_message(array('url' => __CLASS__, 'type' => 'success', 'message' => 'message_save_success'));
                } else {
                    get_validation_errors();
                }
            }

            $this->themes->render('categories/additional_data', $pass);
            return;
        }
        $this->themes->set_message(array('url' => $this->agent->referrer(), 'type' => 'error', 'message' => 'message_store_wrong_link'));
    }

    public function no_trash($id = false) {
        if ($id) {
            $falid_id = $this->cats->is_found(array('id' => $id, 'trash' => 'YES'), array(), 'row', $this->agent->referrer());
            $data['trash'] = 'NO';
            if ($this->cats->save($data, array('id' => $id, 'trash' => 'YES'), TRUE)) {
                $ref = $this->agent->referrer();
                $this->themes->set_message(array('url' => $ref, 'type' => 'success', 'message' => 'message_delete_success'));
            } else {
                $ref = $this->agent->referrer();
                $this->themes->set_message(array('url' => $ref, 'type' => 'error', 'message' => 'message_delete_error'));
            }
        } else {
            show_404();
        }
    }

    public function delete_final($id = false) {
        if ($id) {
            $falid_id = $this->cats->is_found(array('id' => $id, 'trash' => 'YES'), array(), 'row', $this->agent->referrer());
            if ($this->cats->delete(array('id' => $id), FALSE)) {
                $ref = $this->agent->referrer();
                $this->themes->set_message(array('url' => $ref, 'type' => 'success', 'message' => 'message_delete_success'));
            } else {
                $ref = $this->agent->referrer();
                $this->themes->set_message(array('url' => $ref, 'type' => 'error', 'message' => 'message_delete_error'));
            }
        } else {
            show_404();
        }
    }

    public function delete($id = FALSE) {
        if ($id) {
            $falid_id = $this->cats->is_found(array('id' => $id), array(), 'row', __CLASS__);
            if ($this->cats->delete(array('id' => $id), array('parent' => $id))) {
                $this->themes->set_message(array('url' => $this->agent->referrer(), 'type' => 'success', 'message' => 'message_delete_success'));
            } else {
                $this->themes->set_message(array('url' => $this->agent->referrer(), 'type' => 'error', 'message' => 'message_delete_error'));
            }
        } else {
            show_404();
        }
    }

    public function trash($offset = false) {
        $limit = metohd_get_uri('dblimit', ADMIN_LIMIT);
        $count = $this->cats->filter(TRUE, FALSE, TRUE, array('parent' => '0'));
        $this->pass['count'] = $count;
        $this->pass['limit'] = $limit;
        $this->pass['result'] = $this->cats->filter(FALSE, $offset, TRUE, array('parent' => '0'));
        $this->pass['pagination'] = $this->admin_pagination('index', $count, $limit);
        $this->themes->render('categories/trash', $this->pass);
    }

}

?>
